﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Net .Mail;

namespace Device
{
    public class Util
    {
        public enum Result
        {
            Success, OverTime,Login
        }
        //发送找回密码的邮件
        public static void sendEmail(string name, string email, string time,Int32 userid)
        {
            string Str1 = "您收到这封这封电子邮件是因为您 (也可能是某人冒充您的名义) 申请了一个新的密码.假如这不是您本人所申请, 请不用理会这封电子邮件, 但是如果您持续收到这类的信件骚扰, 请您尽快联络管理员";
            string Str2 = "要使用新的密码, 请使用以下链接启用密码.";
            string Str3 = "(如果无法点击该URL链接地址,请将它复制并粘帖到浏览器的地址输入框,然后单击回车即可.该链接使用后将立即失效.)";
            string Str4 = " 注意:请您在收到邮件1个小时内使用,否则该链接将会失效.";
            string Str5 = "我们将一如既往、热忱的为您服务!";
            string Str6 = "用户服务支持：webmaster@csdn.net";
            string url = "http://localhost:3968/ResetPassword.aspx?username=" + name + "&time=" + time + "&userid=" + userid;
            string to = email;
            string from = "13821038913@163.com";
            MailMessage message = new MailMessage(from, to);
            message.SubjectEncoding = System.Text.Encoding.Default;
            message.BodyEncoding = System.Text.Encoding.Default;
            message.Subject = "找回密码";
            message.Body = "尊敬的" + name + "您好!" + "<br>" + Str1 + "<br>" + Str2 + "<br>" + "<a href='" + url + "'>" + url + "</a>" + "<br>" + Str3 + "<br>" + Str4 + "<br>" + Str5 + "<br>" + Str6;
            message.IsBodyHtml = true;
            SmtpClient client = new SmtpClient();
            client.Host = "smtp.163.com";
            client.Credentials = new NetworkCredential("13821038913", "Nanwang2008");
            client.Send(message);
        }


        //发送注册邮箱的邮件
        public static void registerSendEmail(string name, string email, string time, Int32 userid)
        {
            string Str1 = "您收到这封这封电子邮件是因为您 (也可能是某人冒充您的名义) 注册了一个设备.假如这不是您本人所申请, 请不用理会这封电子邮件, 但是如果您持续收到这类的信件骚扰, 请您尽快联络管理员";
            string Str2 = "注册设备, 请使用以下链接激活.";
            string Str3 = "(如果无法点击该URL链接地址,请将它复制并粘帖到浏览器的地址输入框,然后单击回车即可.该链接使用后将立即失效.)";
            string Str4 = " 注意:请您在收到邮件1个小时内使用,否则该链接将会失效.";
            string Str5 = "我们将一如既往、热忱的为您服务!";
            string Str6 = "用户服务支持：webmaster@csdn.net";
            string url = "http://localhost:3968/DeviceMaster.aspx?username=" + name + "&time=" + time + "&userid=" + userid;
            string to = email;
            string from = "13821038913@163.com";
            MailMessage message = new MailMessage(from, to);
            message.SubjectEncoding = System.Text.Encoding.Default;
            message.BodyEncoding = System.Text.Encoding.Default;
            message.Subject = "找回密码";
            message.Body = "尊敬的" + name + "您好!" + "<br>" + Str1 + "<br>" + Str2 + "<br>" + "<a href='" + url + "'>" + url + "</a>" + "<br>" + Str3 + "<br>" + Str4 + "<br>" + Str5 + "<br>" + Str6;
            message.IsBodyHtml = true;
            SmtpClient client = new SmtpClient();
            client.Host = "smtp.163.com";
            client.Credentials = new NetworkCredential("13821038913", "Nanwang2008");
            client.Send(message);
        }

        //提示、报错
        public static void AlertString(string content, HttpResponse Response)
        {
            Response.Write("<script language='javascript'>alert(" + "'" + content + "'" + ");</script>"); 
        }
       
        //提取url中用户名参数
        public static string UserName()
        {
            string name = HttpContext.Current.Request.QueryString["username"];
            return name;
        }

        //提取时间中的参数
        public static string UserTime()
        {
            string time = HttpContext.Current.Request.QueryString["time"];
            if (time == null)
            {
                return "abc";
            }
            else
            {
                return time;
            }
        }

        //提取url中的用户ID
        public static string  UserId()
        {
            string id = HttpContext.Current.Request.QueryString["userid"];
            return id;
        }

        //判断是否超时
        public static Result OverTime(DateTime nowtime)
        {
            var time = UserTime();                                 
            if (time=="abc")
            {
                return Result.Login;
            }
            else
            {
                DateTime oldtime = DateTime.Parse(time);
                if (oldtime >= nowtime)
                {
                    return Result.Success;
                }
                else
                {
                    return Result.OverTime;
                }                
            }
        }
    }

}